﻿''' <summary>Miscaleneous functions</summary>
Module Misc
    ''' <summary>Name of DataDIrectory item for <see cref="AppDomain.GetData"/> and <see cref="AppDomain.SetData"/></summary>
    Public Const DataDirectory As String = "DataDirectory"
    ''' <summary>Default name of database file</summary>
    Public Const ĐŘječniksdf As String = "ĐŘječnik.sdf"
    ''' <summary>Default name of icons' folder</summary>
    Public Const Icons As String = "Icons"
    ''' <summary>Name of empty database file</summary>
    Public Const Emptysdf As String = "Empty.sdf"

    ''' <summary>Regular expression to match @-mail address</summary>
    ''' <remarks><c>^[a-zA-z0-9]([a-zA-Z0-9\-_]\.?)*@[a-zA-z0-9]([a-zA-Z0-9\-_]\.?)*$</c></remarks>
    Public MailMatch As New System.Text.RegularExpressions.Regex("^[a-zA-z0-9]([a-zA-Z0-9\-_]\.?)*@[a-zA-z0-9]([a-zA-Z0-9\-_]\.?)*$", System.Text.RegularExpressions.RegexOptions.Compiled Or System.Text.RegularExpressions.RegexOptions.CultureInvariant Or System.Text.RegularExpressions.RegexOptions.ExplicitCapture)
    ''' <summary>Regular expression to match web adres (http or https)</summary>
    ''' <remarks><c>^(https?|ftp)\://[a-zA-Z0-9]([a-zA-Z0-9\-_].?)*(/[^\?]*)?(\?.*)?$</c></remarks>
    Public WWWMatch As New System.Text.RegularExpressions.Regex("^(https?|ftp)\://[a-zA-Z0-9]([a-zA-Z0-9\-_].?)*(/[^\?]*)?(\?.*)?$", System.Text.RegularExpressions.RegexOptions.Compiled Or System.Text.RegularExpressions.RegexOptions.ExplicitCapture Or System.Text.RegularExpressions.RegexOptions.CultureInvariant)
    ''' <summary>Regular exception to match version number according to DictionarySimple.xsd</summary>
    ''' <remarks><c>^(0|([1-9][0-9]*))\.(0|([1-9][0-9]*))(\.(0|([1-9][0-9]*)))?(\.(0|([1-9][0-9]*)))?$</c></remarks>
    Public VersionMatch As New System.Text.RegularExpressions.Regex("^(0|([1-9][0-9]*))\.(0|([1-9][0-9]*))(\.(0|([1-9][0-9]*)))?(\.(0|([1-9][0-9]*)))?$", System.Text.RegularExpressions.RegexOptions.Compiled Or System.Text.RegularExpressions.RegexOptions.CultureInvariant)
    ''' <summary>Gets location of dictionary database file</summary>
    ''' <param name="Loc">Predefined location</param>
    ''' <param name="UserLoc">User defined location</param>
    ''' <returns>Path of database file acording to <paramref name="Loc"/>. If <paramref name="Loc"/> is <see cref="enmDatabaseLocations.UserDefined"/> then returns <paramref name="UserLoc"/></returns>
    Public Function StorageLocation(ByVal Loc As enmDatabaseLocations, ByVal UserLoc As String) As String
        Select Case Loc
            Case enmDatabaseLocations.CommonApplicationData
                If My.Application.IsNetworkDeployed Then
                    Dim Attrs As System.Reflection.AssemblyFileVersionAttribute() = System.Reflection.Assembly.GetCallingAssembly.GetCustomAttributes(GetType(System.Reflection.AssemblyFileVersionAttribute), True)
                    Dim v As String
                    If Attrs IsNot Nothing AndAlso Attrs.Length > 0 Then
                        v = Attrs(0).Version
                    Else
                        v = My.Application.Info.Version.ToString
                    End If
                    Return Tools.IOt.Path.SystemPaths.AllUsersApplicationData + My.Application.Info.ProductName + My.Application.Info.Title + v + ĐŘječniksdf
                Else
                    Return IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, ĐŘječniksdf)
                End If
            Case enmDatabaseLocations.ProgramFiles
                Return IO.Path.Combine(AppDomain.CurrentDomain.GetData(DataDirectory), ĐŘječniksdf)
            Case enmDatabaseLocations.UserApllicationData
                If My.Application.IsNetworkDeployed Then
                    Return IO.Path.Combine(My.Application.Deployment.DataDirectory, ĐŘječniksdf)
                Else
                    Return IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData, ĐŘječniksdf)
                End If
            Case Else 'enmDatabaseLocations.UserDefined
                Return UserLoc
        End Select
    End Function
    ''' <summary>Gets directory where icons are stored</summary>
    ''' <param name="Loc">Predefined location</param>
    ''' <param name="UserLoc">User defined location</param>
    ''' <returns>Path directory to store icons<paramref name="Loc"/>. If <paramref name="Loc"/> is <see cref="enmDatabaseLocations.UserDefined"/> then returns <paramref name="UserLoc"/></returns>
    Public Function IconsLocation(ByVal Loc As enmDatabaseLocations, ByVal UserLoc As String) As String
        Select Case Loc
            Case enmDatabaseLocations.CommonApplicationData
                If My.Application.IsNetworkDeployed Then
                    Dim Attrs As System.Reflection.AssemblyFileVersionAttribute() = System.Reflection.Assembly.GetCallingAssembly.GetCustomAttributes(GetType(System.Reflection.AssemblyFileVersionAttribute), True)
                    Dim v As String
                    If Attrs IsNot Nothing AndAlso Attrs.Length > 0 Then
                        v = Attrs(0).Version
                    Else
                        v = My.Application.Info.Version.ToString
                    End If
                    Return Tools.IOt.Path.SystemPaths.AllUsersApplicationData + My.Application.Info.ProductName + My.Application.Info.Title + v + Icons
                Else
                    Return IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, Icons)
                End If
            Case enmDatabaseLocations.ProgramFiles
                Return IO.Path.Combine(AppDomain.CurrentDomain.GetData(DataDirectory), Icons)
            Case enmDatabaseLocations.UserApllicationData
                If My.Application.IsNetworkDeployed Then
                    Return IO.Path.Combine(My.Application.Deployment.DataDirectory, Icons)
                Else
                    Return IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData, Icons)
                End If
            Case Else 'enmDatabaseLocations.UserDefined
                Return UserLoc
        End Select
    End Function
    ''' <summary>Returns current loaction of dictionary in database according to <see cref="My.Settings"/></summary>
    Public Function StorageLocation() As String
        Return StorageLocation(My.Settings.StorageLocation, My.Settings.StorageUserDefinedLocation)
    End Function
    ''' <summary>Returns current loaction of directory to store icons according to <see cref="My.Settings"/></summary>
    Public Function IconsLocation() As String
        Return IconsLocation(My.Settings.IconLocation, My.Settings.IconUserDefinedLocation)
    End Function
    ''' <summary>Gets connection string to dictionarry database according to current settings of application</summary>
    Public Function ConnectionString() As String
        Return ConnectionString(StorageLocation)
    End Function
    ''' <summary>Gets connection string to given data source file</summary>
    ''' <param name="DataSource">File to connect to</param>
    ''' <returns>Connection string to sdf file <paramref name="DataSource"/></returns>
    Public Function ConnectionString(ByVal DataSource As String) As String
        Return String.Format("Data Source ='{0}'", DataSource)
    End Function
    ''' <summary>Tries connection to given file</summary>
    ''' <param name="File">File to try connection to</param>
    ''' <returns>True if connection is succesfull, false otherwise</returns>
    Public Function TestConnection(ByVal File As String) As Boolean
        Dim conn As New SqlServerCe.SqlCeConnection(ConnectionString(File))
        Try
            conn.Open()
            Return True
        Catch ex As SqlServerCe.SqlCeException
            Return False
        Finally
            Try
                conn.Close()
            Catch:End Try
        End Try
    End Function
    ''' <summary>Returns curent location to store icons in. See <see cref="IconsLocation"/>.</summary>
    Public ReadOnly Property IconFolder() As Tools.IOt.Path
        Get
            Return IconsLocation()
        End Get
    End Property
#Region "Release quality info"
#If Nightly Then
    ''' <summary>Text shown in title bar when application is compiled as Nightly build</summary>
    Public ReadOnly VInfo$ = My.Resources.Nightly
#ElseIf Alpha Then
    ''' <summary>Text shown in title bar when application is compiled as Alpha build</summary>
    Public  ReadOnly VInfo$ = My.Resources.Alpha
#ElseIf Beta Then
    ''' <summary>Text shown in title bar when application is compiled as Beta build</summary>
    Public ReadOnly VInfo$ = My.Resources.Beta
#ElseIf RC Then
    ''' <summary>Text shown in title bar when application is compiled as Release Candidate build</summary>
    Public ReadOnly VInfo$ = My.Resources.ReleaseCandidate
#ElseIf Release Then
    ''' <summary>Text shown in title bar when application is compiled as Release build</summary>
    Public ReadOnly VInfo$ = My.Resources.Final
#End If
#End Region
End Module